package com.twb.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.base.service.impl.BaseService;
import com.twb.core.cache.redis.RedisUtil;
import com.twb.core.exception.ServiceException;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.ExcelUtils;
import com.twb.core.util.TwbPageUtil;
import com.twb.system.entity.twbSysconfig.TwbSysconfig;
import com.twb.system.entity.twbSysconfig.dto.SearchTwbSysconfigDto;
import com.twb.system.entity.twbSysconfig.vo.SaveTwbSysconfigVo;
import com.twb.system.entity.twbSysconfig.vo.SearchTwbSysconfigVo;
import com.twb.system.entity.twbSysconfig.vo.UpdateTwbSysconfigByIdVo;
import com.twb.system.mapper.TwbSysconfigMapper;
import com.twb.system.service.ITwbSysconfigService;
import com.twb.system.util.ExcelDataDicHandlerImpl;

/**
 * @ClassName TwbSysconfigService
 * @Description 系统配置service
 * @author DSY
 * @date 2018年11月9日 上午9:42:02
 */
@Service("twbSysconfigService")
public class TwbSysconfigService extends BaseService<TwbSysconfigMapper,TwbSysconfig> implements ITwbSysconfigService {

	@Autowired
	TwbSysconfigMapper twbSysconfigMapper;

	@Autowired
	private RedisUtil redisUtil;
	
	/**
	 * @Title getSysConfig
	 * @Description 设置缓存前缀
	 * @return
	 * @author DSY
	 * @date 2021年12月25日 下午4:58:56
	 */
	public String getSysConfig() {
		String cachePrefix = this.twbConfigProperties.getCachePrefix();
		return cachePrefix+":sysconfig";
	}

	@Override
	public boolean initRedisSysConfig() {
		boolean bl = false;

		Map<String, String> map = new HashMap<String, String>();

		TwbSysconfig record = new TwbSysconfig();
		record.setIsdel(0);
		List<TwbSysconfig> queryForList = this.list(record);
		for (TwbSysconfig twsysconfig : queryForList) {
			map.put(twsysconfig.getName(), twsysconfig.getSysrule());
		}
		redisUtil.set(getSysConfig(), map);

		bl = true;
		return bl;
	}

	@Override
	public Map<String, String> getRedisSysConfig() {
		Map<String, String> map = redisUtil.get(getSysConfig());
		if (null == map || map.isEmpty()) {
			boolean init = initRedisSysConfig();
			if (init) {
				map = redisUtil.get(getSysConfig());
			}
		}
		return map;
	}

	@Override
	public TwbPageInfo<SearchTwbSysconfigDto> searchTwbSysconfig(SearchTwbSysconfigVo vo) {
		TwbPageUtil.startPage(vo);
		List<SearchTwbSysconfigDto> list = twbSysconfigMapper.searchTwbSysconfig(vo);

		return TwbPageUtil.of(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int saveTwbSysconfig(@Valid SaveTwbSysconfigVo vo) {
		TwbSysconfig record = new TwbSysconfig();
		BeanUtils.copyNotNullProperties(vo, record);
		record.setId(this.getUUID());
		this.setCreateInfo(record);
		int result = twbSysconfigMapper.insert(record);
		if (result > 0) {
			initRedisSysConfig();
		}
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateTwbSysconfigById(UpdateTwbSysconfigByIdVo vo) {
		TwbSysconfig sysconfig = twbSysconfigMapper.selectById(vo.getId());
		if (null == sysconfig) {
			throw new ServiceException("2001", "系统配置不存在");
		}
		BeanUtils.copyProperties(vo, sysconfig);
		this.setUpdateInfo(sysconfig);
		int result = twbSysconfigMapper.updateById(sysconfig);
		if (result > 0) {
			initRedisSysConfig();
		}
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int deleteTwbSysconfigById(String id) {
		int result = twbSysconfigMapper.deleteById(id);
		if (result > 0) {
			initRedisSysConfig();
		}
		return result;
	}

	@Override
	public void exportExcel(SearchTwbSysconfigVo vo, HttpServletResponse response) {
		TwbPageInfo<SearchTwbSysconfigDto> pageInfo = this.searchTwbSysconfig(vo);
		List<SearchTwbSysconfigDto> list = pageInfo.getList();
		ExcelDataDicHandlerImpl diceAddressListHandlerImpl = new ExcelDataDicHandlerImpl();
		String fileName = vo.getFileName();
		if(StringUtils.isBlank(fileName)) {
			fileName="系统配置";
		}
		ExcelUtils.exportExcel(fileName, list, SearchTwbSysconfigDto.class, response,diceAddressListHandlerImpl);
	}
}
